//
//  MTCacheService.m
//  mt_ios
//
//  Created by cherry pu on 17/1/17.
//  Copyright © 2017年 miTing. All rights reserved.
//

#import "MBCacheService.h"

static MBCacheService* cacheService;

@interface MBCacheService ()
@property (nonatomic,strong)NSMutableDictionary* libraryPhotosCache;
@property (nonatomic,strong) NSMutableDictionary* headerCache;
@property (nonatomic,strong) NSMutableDictionary* bannerImageCache;

@end

@implementation MBCacheService

+(instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        cacheService = [super allocWithZone:zone];
    });
    return cacheService;
}

+ (instancetype)sharedInstance
{
    return [[self alloc]init];
}


#pragma mark - lazy load
-(NSMutableDictionary *)libraryPhotosCache
{
    if (_libraryPhotosCache == nil) {
        _libraryPhotosCache = [NSMutableDictionary dictionary];
    }
    return _libraryPhotosCache;
}

-(NSMutableDictionary *)bannerImageCache
{
    if (_bannerImageCache == nil) {
        _bannerImageCache = [NSMutableDictionary dictionary];
    }
    return _bannerImageCache;
}

-(NSMutableDictionary *)headerCache
{
    if (_headerCache == nil) {
        _headerCache = [NSMutableDictionary dictionary];
    }
    return _headerCache;
}

/**
 *  缓存相册图片
 *
 *  @param libraryName 名称
 *  @param photosArray 相册
 */
-(void)cachePhotosWithLibraryName:(NSString*)libraryName withPhotosArray:(NSMutableArray *)photosArray
{
    NSMutableArray* newArray = [photosArray mutableCopy];
    if (libraryName && photosArray) {
        [self.libraryPhotosCache setObject:newArray forKey:libraryName];
    }
}

/**
 *  获取相册图片
 *
 *  @param libraryName 相册名称
 *
 *  @return 相册
 */
-(NSMutableArray*)fetchPhotosWithLibraryName:(NSString*)libraryName
{
    NSMutableArray* photos;
    if (libraryName) {
        photos = [self.libraryPhotosCache objectForKey:libraryName];
    }
    return photos;
}


/**
 缓存头像
 
 @param headerName 头像地址
 @param header 头像
 */
-(void)cacheHeaderWithHeaderName:(NSString*)headerName withHeader:(UIImage*)header
{
    if (header && headerName) {
        [self.headerCache setObject:header forKey:headerName];
    }

}


/**
 获取头像
 
 @param headerName 头像地址
 @return 头像
 */
-(UIImage*)fetchHeaderWithHeaderName:(NSString*)headerName
{
    UIImage* header;
    if (headerName) {
        header = [self.headerCache objectForKey:headerName];
    }
    return header;
}

/**
 获取banner图片
 
 @param imageUrl 图片地址
 @return 图片
 */
-(UIImage*)fetchBannerImageWithUrl:(NSString*)imageUrl
{
    UIImage* image;
    if (imageUrl) {
        image = [self.bannerImageCache objectForKey:imageUrl];
    }
    return image;
}

/**
 *  缓存banner图片
 *
 *  @param url   地址
 *  @param image 图片
 */
-(void)cacheBannerImageWithUrl:(NSString*)url andImage:(UIImage *)image
{
    if (url && image) {
        [self.bannerImageCache setObject:image forKey:url];
    }
}


@end
